package com.jfinal.club._admin.banji;

import java.util.List;
import com.jfinal.club.common.model.Banji;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

public class BanjiAdminService {

	private Banji dao = new Banji().dao();
	
	public Page<Banji> paginate(int pageNum) {
		return dao.paginate(pageNum, 10, "select banji.id,banji.clname,college.coname ",
				"from banji left join college on banji.coid=college.id order by banji.id desc");
	}
	
	public boolean exists(int banjiId, String name) {
		name = name.toLowerCase().trim();
		String sql = "select id from banji where banjiname = ? and id != ? limit 1";
		Integer id = Db.queryInt(sql, name, banjiId);
		return id != null;
	}
	
	public List<Banji> getAll() {
		List<Banji> list = dao.find("select * from banji order by clname");
		return list;
	}
	
	public Ret save(int id, Banji banji) {
//		area.setId(id);
		banji.setClname(banji.getClname().trim());
		banji.save();
		return Ret.ok("msg", "创建成功");
	}
	
	public Banji edit(int id) {
		return dao.findById(id);
	}
	
	public Ret update(Banji banji) {
		if (exists(banji.getId(), banji.getClname())) {
			return Ret.fail("msg", "班级已经存在");
		}

		banji.setClname(banji.getClname().trim());
		banji.update();
		return Ret.ok("msg", "修改成功");
	}
	
	public Ret delete(int banjiId) {
		dao.deleteById(banjiId);
		return Ret.ok("msg", "班级删除成功");
	}
}
